﻿// <copyright file="XmlItemDataReaderMappingTest.cs" company="Oleg V. Polikarpotchkin">
// Copyright © 2008-2009 Oleg V. Polikarpotchkin. All Right Reserved
// </copyright>
// <author>Oleg V. Polikarpotchkin</author>
// <email>ov-p@yandex.ru</email>
// <date>2009-04-12</date>
// <summary>WPF OpenWPFChart library. Helpers.XmlItemDataReader.Mapping class Test.</summary>
// <revision>$Id$</revision>

using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenWPFChart.Helpers.XmlItemDataReader;

namespace Tests
{
    /// <summary>
	/// This is a test class for <see cref="OpenWPFChart.Helpers.XmlItemDataReader.Mapping"/> class
	/// and is intended to contain all Mapping Unit Tests
    ///</summary>
	[TestClass()]
	public class XmlItemDataReaderMappingTest
	{
		#region TestContext
		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		}
		#endregion TestContext

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		//[ClassInitialize()]
		//public static void MyClassInitialize(TestContext testContext)
		//{
		//}
		//
		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup()
		//{
		//}
		//
		//Use TestInitialize to run code before running each test
		//[TestInitialize()]
		//public void MyTestInitialize()
		//{
		//}
		//
		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup()
		//{
		//}
		//
		#endregion

		/// <summary>
		/// Successfull Load test.
		///</summary>
		[TestMethod()]
		public void LoadTest()
		{
			string xmlMappings = @"<?xml version='1.0' encoding='utf-8'?>
			<Mappings xmlns='urn:ItemData-mapping'>
				<Mapping>
					<XAxis xsd-type='gMonth' clr-type='System.Int32' type-string='Month'/>
					<YAxis xsd-type='int' clr-type='System.Int32' type-string='Int'/>
				</Mapping>
				<Mapping>
					<XAxis xsd-type='gMonth' clr-type='System.Int32' type-string='Month'/>
					<YAxis xsd-type='double' clr-type='System.Double' type-string='Double'/>
				</Mapping>
				<Mapping>
					<XAxis xsd-type='gMonth' clr-type='System.Int32' type-string='Month'/>
					<YAxis xsd-type='dateTime' clr-type='System.DateTime' type-string='DateTime'/>
				</Mapping>
				<Mapping>
					<XAxis xsd-type='gMonth' clr-type='System.Int32' type-string='Month'/>
					<YAxis xsd-type='date' clr-type='System.DateTime' type-string='Date'/>
				</Mapping>
				<Mapping>
					<XAxis xsd-type='gMonth' clr-type='System.Int32' type-string='Month'/>
					<YAxis xsd-type='gMonth' clr-type='System.Int32' type-string='Month'/>
				</Mapping>
			</Mappings>";

			using (StringReader sr = new StringReader(xmlMappings))
			{
				List<Mapping> mappings = Mapping.Load(sr);
				Assert.IsNotNull(mappings);
				Assert.AreEqual(5, mappings.Count());
				for (int i = 0; i < mappings.Count; i++)
				{
					Mapping mapping = mappings[i];
					switch (i)
					{
						case 0:
							Assert.AreEqual("gMonth", mapping.Axes[0].XsdTypeString);
							Assert.AreEqual("System.Int32", mapping.Axes[0].ClrType.FullName);
							Assert.AreEqual("Month", mapping.Axes[0].TypeString);
							Assert.AreEqual("int", mapping.Axes[1].XsdTypeString);
							Assert.AreEqual("System.Int32", mapping.Axes[1].ClrType.FullName);
							Assert.AreEqual("Int", mapping.Axes[1].TypeString);
							break;
						case 1:
							Assert.AreEqual("gMonth", mapping.Axes[0].XsdTypeString);
							Assert.AreEqual("System.Int32", mapping.Axes[0].ClrType.FullName);
							Assert.AreEqual("Month", mapping.Axes[0].TypeString);
							Assert.AreEqual("double", mapping.Axes[1].XsdTypeString);
							Assert.AreEqual("System.Double", mapping.Axes[1].ClrType.FullName);
							Assert.AreEqual("Double", mapping.Axes[1].TypeString);
							break;
						case 2:
							Assert.AreEqual("gMonth", mapping.Axes[0].XsdTypeString);
							Assert.AreEqual("System.Int32", mapping.Axes[0].ClrType.FullName);
							Assert.AreEqual("Month", mapping.Axes[0].TypeString);
							Assert.AreEqual("dateTime", mapping.Axes[1].XsdTypeString);
							Assert.AreEqual("System.DateTime", mapping.Axes[1].ClrType.FullName);
							Assert.AreEqual("DateTime", mapping.Axes[1].TypeString);
							break;
						case 3:
							Assert.AreEqual("gMonth", mapping.Axes[0].XsdTypeString);
							Assert.AreEqual("System.Int32", mapping.Axes[0].ClrType.FullName);
							Assert.AreEqual("Month", mapping.Axes[0].TypeString);
							Assert.AreEqual("date", mapping.Axes[1].XsdTypeString);
							Assert.AreEqual("System.DateTime", mapping.Axes[1].ClrType.FullName);
							Assert.AreEqual("Date", mapping.Axes[1].TypeString);
							break;
						case 4:
							Assert.AreEqual("gMonth", mapping.Axes[0].XsdTypeString);
							Assert.AreEqual("System.Int32", mapping.Axes[0].ClrType.FullName);
							Assert.AreEqual("Month", mapping.Axes[0].TypeString);
							Assert.AreEqual("gMonth", mapping.Axes[1].XsdTypeString);
							Assert.AreEqual("System.Int32", mapping.Axes[1].ClrType.FullName);
							Assert.AreEqual("Month", mapping.Axes[1].TypeString);
							break;
					}
				}
			}
		}

		/// <summary>
		/// Test for Load with Recurring Entries.
		///</summary>
		[TestMethod()]
		[ExpectedException(typeof(RecurringMappingEntriesException))]
		public void RecurringEntriesLoadTest()
		{
			string xmlMappings = @"<?xml version='1.0' encoding='utf-8'?>
			<Mappings xmlns='urn:ItemData-mapping'>
				<Mapping>
					<XAxis xsd-type='gMonth' clr-type='System.Int32' type-string='Month'/>
					<YAxis xsd-type='int' clr-type='System.Int32' type-string='Int'/>
				</Mapping>
				<Mapping>
					<XAxis xsd-type='gMonth' clr-type='System.Int32' type-string='Month'/>
					<YAxis xsd-type='double' clr-type='System.Double' type-string='Double'/>
				</Mapping>
				<Mapping>
					<XAxis xsd-type='gMonth' clr-type='System.Int32' type-string='Month'/>
					<YAxis xsd-type='double' clr-type='System.Double' type-string='Double'/>
				</Mapping>
			</Mappings>";

			using (StringReader sr = new StringReader(xmlMappings))
			{
				List<Mapping> mappings = Mapping.Load(sr);
				Assert.IsNotNull(mappings);
				Assert.AreEqual(0, mappings.Count());
			}
		}
	}
}
